var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');

var app = getApp();

Page({
  data: {
    tmChemReagents: [],
    experimentName: '',
    apNum: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    let dataDetails = options.dataString;
    let tmChemReagents = JSON.parse(decodeURIComponent(dataDetails));

    this.setData({
      tmChemReagents: tmChemReagents
    });

  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示
    
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  // 实验名称输入绑定
  bindExperimentNameInput: function (e) {
    this.setData({
      experimentName: e.detail.value
    });
  },
  // 申请剂量结束输入
  applyNumChange: function(e){
    // 获取选中下标位置
    let itemIndex = e.target.dataset.itemIndex;
    let tmpTmChemReagents = this.data.tmChemReagents;

    // 申请剂量不能大于库存剂量
    if (parseInt(e.detail.value) > parseInt(tmpTmChemReagents[itemIndex].qty)) {
      util.showErrorToast('不能大于库存');
      this.setData({
        apNum: ''
      });
      return false;
    }

    // 赋值
    tmpTmChemReagents[itemIndex].applyNum = e.detail.value;

    this.setData({
      tmChemReagents: tmpTmChemReagents
    });
  },
  // 是否归还绑定
  isRevertChange: function(e){
    // 获取选中下标位置
    let itemIndex = e.target.dataset.itemIndex;
    let tmpTmChemReagents = this.data.tmChemReagents;
    
    // 是为Y，否则为N
    if (e.detail.value){
      tmpTmChemReagents[itemIndex].isRevert = 'Y';
    } else {
      tmpTmChemReagents[itemIndex].isRevert = 'N';
    }

    this.setData({
      tmChemReagents: tmpTmChemReagents
    });
  },
  // 返回继续添加试剂
  continueAppend: function () {
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面

    var reagentIds = this.data.tmChemReagents.map(function (v) {
      return v.reagentId;
    });

    prevPage.setData({//直接给上移页面赋值
      reagentIds: reagentIds,
      continueAppend: 1
    });
    wx.navigateBack({//返回
      delta: 1
    })
  },
  // 扫码添加试剂
  scanAppend: function () {
    var that = this;

    wx.scanCode({
      success(res) {
        // http://www.baidu.com?id=14
        console.log(res.result);
        let results = res.result.split('=');
        let reagengId = results[1];

        util.request(api.ChemreagentDetail, {reagentId: reagengId })
        .then(function (res) {
          if (res.code === 0) {
            // 扫码添加试剂是否已存在
            let isExit = false;

            let tempChemReagents = that.data.tmChemReagents.map(function (v) {
              if (v.reagentId == res.tmChemReagentEntity.reagentId) {
                isExit = true;
              }
              return v;
            });
            // 不存在，添加到列表中
            if (!isExit) {
              tempChemReagents.push(res.tmChemReagentEntity);

              that.setData({
                tmChemReagents: tempChemReagents
              });
            } else {
              util.showErrorToast('该试剂已存在');
            }
          }
        });
      }
    });
  },
  // 提交申请
  commitApply: function () {
    //获取已选择的商品
    let that = this;

    if (that.data.experimentName == null || that.data.experimentName == "") {
      util.showErrorToast('请输入实验名称');
      return false;
    }
    let tempChem = this.data.tmChemReagents;

    if (tempChem.length <= 0) {
      util.showErrorToast('未选择实验试剂');
      return false;
    }
    // 是否归还
    let IfRe = 'N';

    // 获取选择试剂信息和对应数量、是否归还
    var reagentIds = this.data.tmChemReagents.map(function (v) {
      if (v.isRevert == 'Y') {
        IfRe = 'Y';
      }
      return {reagentId: v.reagentId, applyNum: v.applyNum, isRevert: v.isRevert};
    });
    // 转换json格式传输
    let jsonText = JSON.stringify(reagentIds);

    util.request(api.ReagentApply, {
      reagentIdJson: jsonText,
      IfRe : IfRe,
      experimentName: that.data.experimentName
    }).then(function (res) {
      if (res.code === 0) {
        wx.redirectTo({
          url: '/pages/cr/businessHand/reagentApply/reagentApply'
        });
      } else {
        util.showErrorToast('提交失败');
      }
    }).catch(res => {
      util.showErrorToast('提交失败');
    });
  }
})